package com.atguigu.springcloud.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author : wenchao.long
 * @date : Created in 2020/12/15 8:20 周二
 * @description :
 */
@Service
public class PaymentService {

    public String paymentInfo(Integer id) {
        return "线程池：" + Thread.currentThread().getName() + "paymentInfo, id：" + id + "\t" + "O(∩_∩)O哈哈~";
    }

    @HystrixCommand(fallbackMethod = "paymentInfoTimeoutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
    })
    public String paymentInfoTimeout(Integer id) {
        int timeout = 5;
        try {
            TimeUnit.SECONDS.sleep(timeout);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "线程池：" + Thread.currentThread().getName()
                + "paymentInfo, id：" + id + "\t" + "O(∩_∩)O哈哈~"
                + "耗时(秒)：" + timeout;
    }

    public String paymentInfoTimeoutHandler(Integer id) {
        return "服务降级了" + "o(╥﹏╥)o";
    }

    public String paymentInfoTimeOutClient(Integer id) {
        int timeout = 5;
        try {
            TimeUnit.SECONDS.sleep(timeout);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "线程池：" + Thread.currentThread().getName()
                + "paymentInfo, id：" + id + "\t" + "O(∩_∩)O哈哈~"
                + "耗时(秒)：" + timeout + "client";
    }
}
